﻿using SqlSugar;
using Xunit.Abstractions;

namespace SqlSugarLean;

public class Test001
{
    private readonly ITestOutputHelper _testOutputHelper;

    public Test001(ITestOutputHelper testOutputHelper)
    {
        _testOutputHelper = testOutputHelper;
    }

    [Fact]
    public void Test1()
    {
        var connectionConfig = new ConnectionConfig()
        {
            ConnectionString = @"
                    Server=rm-bp1gvpb1n1hd3d23dqo.mysql.rds.aliyuncs.com;
                    Port=3306;Database=SqlSugar;User=fengliulin;Password=QQwoaini123;charset=utf8mb4",
            DbType = DbType.MySql,
            IsAutoCloseConnection = true
        };
        var db = new SqlSugarClient(connectionConfig, client => client.Aop.OnLogExecuted= (s, parameters) =>
        {
            _testOutputHelper.WriteLine(UtilMethods.GetNativeSql(s, parameters));
        });

        db.DbMaintenance.CreateDatabase();
        db.CodeFirst.InitTables<Student>();

        var students = db.Queryable<Student>().ToList();
        db.Insertable(new Student() { SchoolId = 1, Name = "jack" }).ExecuteCommand();
        db.Updateable(new Student() { Id = 1, SchoolId = 2, Name = "jack2" }).ExecuteCommand();
        db.Deleteable<Student>().Where(student => student.Id == 1).ExecuteCommand();
    }

    public class Student
    {
        //数据是自增需要加上IsIdentity
        //数据库是主键需要加上IsPrimaryKey
        //注意：要完全和数据库一致2个属性
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public int? SchoolId { get; set; }
        public string? Name { get; set; }
    }
}